package com.dzh.controller;

import com.dzh.mapper.*;
import com.dzh.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {
@Autowired
private UserMapper userMapper;
@Autowired
 private   RecommendMapper recommendMapper;
@Autowired
private GalleyMapper galleyMapper;
@Autowired
private AgentMapper agentMapper;
@Autowired
private CommentMapper commentMapper;
@Autowired
private RedisTemplate redisTemplate;
    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }


//首页
    @RequestMapping("")
    public String index(Model model){
        //通过类型列出列表
        String a = "newest";
        List<Recommend> recommends =  recommendMapper.queryGallyByType(a);
        for(Recommend recommend:recommends){
            Galley galley =  galleyMapper.queryGalleyById(recommend.getGid());
            recommend.setGalleys(galley);
        }
        model.addAttribute("newrecommendlist",recommends);

        List<Agent> agents = agentMapper.queryAllAgent();
        model.addAttribute("agents",agents);

       List<Comment> commentlist =  commentMapper.quertAllComment();
       model.addAttribute("commentlist",commentlist);


        return "index";
    }

    //登录界面
    @RequestMapping("/login")
    public String login(Model model,User user,String username,String password, HttpSession session) {

        //通过类型列出列表
        String a = "newest";
        String b = "popular";
        List<Recommend> recommends = recommendMapper.queryGallyByType(a);
        List<Recommend> recommends1 = recommendMapper.queryGallyByType(b);
        for (Recommend recommend : recommends) {
            Galley galley = galleyMapper.queryGalleyById(recommend.getGid());
            recommend.setGalleys(galley);
        }


        for (Recommend recommend : recommends1) {
            Galley galley = galleyMapper.queryGalleyById(recommend.getGid());
            recommend.setGalleys(galley);
        }

        model.addAttribute("newrecommendlist", recommends);
        model.addAttribute("popularcommendlist", recommends1);

        List<Agent> agents = agentMapper.queryAllAgent();
        model.addAttribute("agents", agents);



        String key =username;
        Object obj = redisTemplate.opsForValue().get(key);
        //如果缓存没有
        if (obj == null) {
            User userr = userMapper.queryUserById(user);
            redisTemplate.opsForValue().set(userr.getUsername(), userr);
            if (userr != null) {
                session.setAttribute("loginUser", username);
//            redisTemplate.opsForValue().set(userr.getUsername(),userr.getPassword());
//            System.out.println(redisTemplate.opsForValue().get(userr.getUsername()));
                System.out.println("走了数据库");
                return "index";
            }
            //错误 user没值失败
            else {
                model.addAttribute("msg", "密码有错误");
                return "login";
            }
        }
        // 缓存有
        else {
            // 正确user有值登录
            if (obj != null) {
                session.setAttribute("loginUser", username);
//            redisTemplate.opsForValue().set(userr.getUsername(),userr.getPassword());
//            System.out.println(redisTemplate.opsForValue().get(userr.getUsername()));
                System.out.println("走了缓存");
                return "index";
            }
            //错误 user没值失败
            else {
                model.addAttribute("msg", "密码有错误");
                return "login";
            }
        }
    }



    @RequestMapping("/register")
    public String register(Model model,User user,String username,HttpSession session){
      int o =  userMapper.queryUserByName(username);
      System.out.println(user);
        if(o==0){
            userMapper.addUser(user);
            return "login";
        }
        else{
            model.addAttribute("msg2","该账号有人创建了");
            return "login";
        }


    }

















//    ---------------------------------------------------------------
    //界面转换


    @RequestMapping("/blog")
    public String blog(){
        return "blog";
    }


    @RequestMapping("/blogdetail")
    public String blogdetail(){
        return "blog-detail";
    }



    @RequestMapping("/galley")
    public String galley(){
        return "gallery";
    }

    @RequestMapping("/errorpage")
    public String errorpage(){
        return "error-page";
    }

    @RequestMapping("/service")
    public String service(){
        return "services";
    }

    @RequestMapping("/aboutus")
    public String aboutsus(){
        return "about";
    }


    @RequestMapping("/tosubmit")
    public String submit(){

        return "submit";
    }

    @RequestMapping("/toup")
    public String upload(){
        return "uploaddd";
    }


    @RequestMapping("/contact")
    public String contact(){
        return "contact";
    }





//    @RequestMapping("/selectall")
//    @ResponseBody
//    public List<User> selectall(){
//      List<User> list =  userMapper.queryUser();
//      System.out.println(list);
//        return list;
//    }
}
